package com.example.drss;

import java.util.ArrayList;
import java.util.List;

import com.example.drss.adapter.ListViewCollectionAdapter;
import com.example.drss.bean.Menu;
import com.example.drss.db.DBTool;
import com.example.drss.itemdata.ItemData;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

public class CollectionActivity extends Activity {

	DBTool dbTool;
	String path;
	ListView listView;
	List<ItemData> data ;
	Button backButton;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_collection);
		path=getIntent().getStringExtra("databasePath_collection");
		dbTool=new DBTool(path);
		dbTool.openDatabase();
		init();
	}
	
	void queryCollection(){
		data = new ArrayList<ItemData>();  	
		Cursor cursor=dbTool.queryCollection();
		if (cursor!=null) {
			//读取所有纪录
			List<Menu> menus =new ArrayList<Menu>();
			while(cursor.moveToNext()){
				Menu menu=new Menu();
				menu.setName(cursor.getString(1));
				menu.setImage(cursor.getString(2));
				menus.add(menu);
			}
			for(Menu menu:menus){
				ItemData data1= new ItemData();
				data1.setName(menu.getName());
				data1.setImage(menu.getImage());
				data.add(data1);
			}		
		}
		ListViewCollectionAdapter adapter = new ListViewCollectionAdapter(CollectionActivity.this, data);
		listView.setAdapter(adapter);
	}

	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		dbTool.closeDatabase();
	}

	private void init() {
		// TODO Auto-generated method stub


		listView=(ListView)findViewById(R.id.listView_collection);
		backButton=(Button)findViewById(R.id.button_collection_back);
		queryCollection();
		listView.setOnItemClickListener(new OnItemClickListener() {
			
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				Intent intent=new Intent(CollectionActivity.this,DishesActivity.class);
				ItemData itemData=data.get(position);
				intent.putExtra("databasePath_dishes", path);
				intent.putExtra("name", itemData.getName());
				startActivityForResult(intent, 1);
			}
		});
		backButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
				//onDestroy();
				dbTool.closeDatabase();
			}
		});
		    	
	}	
	 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	    	// TODO Auto-generated method stub
	    	//4 
	    	switch (requestCode) {
			case 1:{
				switch (resultCode) {
				case RESULT_OK:
					break;
				case RESULT_CANCELED:{
					queryCollection();
					}
					break;

				default:
					break;
				}
			}
				break;
						default:
				break;
				
			}
	    	super.onActivityResult(requestCode, resultCode, data);
	    }
}
